package cn.iocoder.yudao.module.shop.convert.price;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserRespVO;
import cn.iocoder.yudao.module.shop.controller.admin.member.vo.MemberUserDetailRespVO;
import cn.iocoder.yudao.module.shop.controller.admin.priceitem.vo.ShopPriceItemBatchCreateReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.priceitem.vo.ShopPriceItemCreateReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.priceitem.vo.ShopPriceItemRespVO;
import cn.iocoder.yudao.module.shop.convert.priceitem.ShopPriceItemConvert;
import cn.iocoder.yudao.module.shop.dal.dataobject.priceitem.ShopPriceItemDO;
import cn.iocoder.yudao.module.system.api.tenant.TenantApi;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import cn.iocoder.yudao.module.shop.controller.admin.price.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.price.ShopPriceDO;
import org.springframework.instrument.classloading.ResourceOverridingShadowingClassLoader;

/**
 * 店内价格套餐 Convert
 *
 * @author 曹建伟
 */
@Mapper
public interface ShopPriceConvert {

    ShopPriceConvert INSTANCE = Mappers.getMapper(ShopPriceConvert.class);

    ShopPriceDO convert(ShopPriceCreateReqVO bean);

    ShopPriceDO convert(ShopPriceUpdateReqVO bean);

    ShopPriceRespVO convert(ShopPriceDO bean);

    List<ShopPriceRespVO> convertList(List<ShopPriceDO> list);

    PageResult<ShopPriceRespVO> convertPage(PageResult<ShopPriceDO> page);

    List<ShopPriceExcelVO> convertList02(List<ShopPriceDO> list);

    default ShopPriceDO convert(String name, Long tenantId){
        if ( name == null ) {
            return null;
        }

        ShopPriceDO.ShopPriceDOBuilder shopPriceDO = ShopPriceDO.builder();

        shopPriceDO.name( name );
        shopPriceDO.tenantId(tenantId);

        return shopPriceDO.build();
    }

    default ShopPriceItemBatchCreateReqVO convertPriceItem(Long priceId, List<Long> itemIdList, List<Integer> priceList, Long tenantId){
        ShopPriceItemBatchCreateReqVO createReqVO = new ShopPriceItemBatchCreateReqVO();

        List<ShopPriceItemCreateReqVO> list = new ArrayList<>();
        for(int i = 0; i<itemIdList.size(); i++){
            Long itemId = itemIdList.get(i);
            Integer price = priceList.get(i);

            ShopPriceItemCreateReqVO createItem = new ShopPriceItemCreateReqVO();
            createItem.setPriceId(priceId);
            createItem.setItemId(itemId);
            createItem.setPrice(price);
            createItem.setTenantId(tenantId);

            list.add(createItem);
        }

        createReqVO.setItems(list);
        createReqVO.setTenantId(tenantId);

        return createReqVO;
    }

    default PageResult<ShopPriceRespVO> convertPage(PageResult<ShopPriceDO> page, Map<Long, List<ShopPriceItemDO>> shopPriceItemDOMap){
        PageResult<ShopPriceRespVO> result =  new PageResult<ShopPriceRespVO>();

        List<ShopPriceRespVO> list = new ArrayList<ShopPriceRespVO>();

        for(ShopPriceDO shopPriceDO: page.getList()){
            ShopPriceRespVO respVO = new ShopPriceRespVO();
            respVO.setId(shopPriceDO.getId());
            respVO.setCreateTime(shopPriceDO.getCreateTime());
            respVO.setName(shopPriceDO.getName());
            respVO.setIsDefault(shopPriceDO.getIsDefault());

            List<ShopPriceItemDO> shopPriceItemDOS = shopPriceItemDOMap.get(shopPriceDO.getId());
            List<ShopPriceItemRespVO> itemList = new ArrayList<>();
            if(shopPriceItemDOS != null){
                for(ShopPriceItemDO shopPriceItemDO : shopPriceItemDOS){
                    itemList.add(ShopPriceItemConvert.INSTANCE.convert(shopPriceItemDO));
                }
            }

            respVO.setPriceItems(itemList);
            list.add(respVO);
        }

        result.setList(list);
        result.setTotal(page.getTotal());

        return result;
    }

}
